<?php
/*** 
 *
 *
 *
 **/
require_once '../db/db.php';
require_once '../util/perm.php';

function postCount( $tid ) { 
  if( !hasPerm('post.view.count') ) 
    return MSG_UNAUTHORIZED;

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();
    $res = $db->query("SELECT COUNT(post.rank) AS pcount FROM thread LEFT JOIN post ON post.stream = thread.stream WHERE thread.id = '$tid';")->fetch(); 
    $cnt = $res['pcount'];
    $res = NULL;
    return $cnt;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
};
function streamSize( $stream ) { 
  if( !hasPerm('post.view.count') ) 
    return MSG_UNAUTHORIZED;

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();
    $res = $db->query("SELECT COUNT(post.rank) AS pcount FROM thread LEFT JOIN post ON post.stream = ${stream};")->fetch(); 
    $cnt = $res['pcount'];
    $res = NULL;
    return $cnt;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
};

// fast

function getFlatPosts( $tid, $rank, $inc = DEFAULT_PAGE_INC ) {
  if( !hasPerm('post.view') ) 
    return MSG_UNAUTHORIZED;

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();
    // XXX pulling ttitle works, but -- it's not accurate if the thread doesn't point to post rank.1
    $posts = $db->query("SELECT post.id AS id, post.stream AS stream, post.title, post.rank, post.date, user.name AS uname, user.id AS uid, post.smurfKey,  post.datum, user.avatar AS avatar, forum.title AS ftitle, forum.id AS fid, (SELECT title FROM post WHERE post.stream = stream AND post.rank = 1) AS ttitle
                         FROM thread
                           LEFT JOIN user, post, forum
                             ON post.uid = user.id
                             AND post.stream = thread.stream
                             AND forum.id = thread.fid
                         WHERE thread.id = '${tid}'
                         ORDER BY post.rank
                         LIMIT $rank, $inc;");
    $ret = $posts->fetchAll( PDO::FETCH_ASSOC );
    $posts = NULL;
    return $ret;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";

    die();
  } 
};

function getThreadedPosts( $pid, $depth ) {
  return -1;
};
function getPost( $pid ) {
  if( !hasPerm('post.view') ) 
    return MSG_UNAUTHORIZED;

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $pid = $db->quote( $pid );

    // XXX don't like this, leaves hanging PDOStatement Object
    $tmp = $db->query("SELECT post.title, post.datum, post.id, post.rank, post.date, user.name AS uname, user.id AS uid, post.smurfKey
                         FROM post
                           LEFT JOIN user
                             ON post.uid = user.id
                           WHERE post.id = ${pid};");
    $data = $tmp->fetch();
    $tmp = NULL;
    return $data;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
};

function getPostsByUid( $uid ) {
  if( !hasPerm('post.view') ) 
    return MSG_UNAUTHORIZED;

  return array();
};


?>
